<?php
namespace app\wechat\controller;

use app\chance\model\ChanceModel;
use app\record\model\RecordModel;
use app\wechat\model\UserInfoModel;
use app\common\base\BasicRest;
use think\Request;
use think\Session;
use think\Db;

class Choujiang extends  BasicRest
{

   protected $beforeActionList = [
       'islogin'
   ];

    // 判断是否登陆
   protected function islogin(){
       if(!Session::has('userid')){
           return $this->redirect('/php/public/wechat/login/login',302);
       }
   }

    public function choujiang(){
       $user=UserInfoModel::get(Session::get('userid'));
       if($user){
           //已经签到了
           if($user->is_check==1){
               // return $this->redirect('/wechat/choujiang/index',302);
                return $this->fetch('index');
           }
           return $this->redirect('/php/public/wechat/qiandao/qiandao',302);
       }
       return $this->redirect('/php/public/wechat/login/login',302);

    }

    //判读用户是否登陆
    public function checkLogin(){
        $loginData = Session::get('userid');
        $res = UserInfoModel::get(['id'=>$loginData]);
        if($res) return res_success($res);
        return res_fail('未找到信息');
    }


    //获取中奖概率
    public function getChance(){
        $res = ChanceModel::all();
        if($res) return res_success($res);
        return res_fail('未找到信息');
    }

    //记录奖项
    public function getPrize(Request $request){
        $loginData = Session::get('userid');
        $iscunzai = RecordModel::get(["uid"=>$loginData]);
        if($iscunzai){
          return res_fail('已经抽过奖了');
        }
        $time = Db::table('time')->where(['id'=>1])->find();
        $now = date('Y-m-d H:i:s');
        if($time['start_time']>$now||$now>$time['end_time']){
          return res_fail('不在抽奖时间内');
        }
        UserInfoModel::where(['id' => $loginData])->update(['is_draw' => 1]);
        $item = $request->post('item');
        switch ($item){
            case 1:
                $str = '未中奖';
                break;
            case 2:
                $str = '一等奖';
                break;
            case 3:
                $str = '二等奖';
                break;
            case 4:
                $str = '三等奖';
                break;
        }

        $res = RecordModel::create(["uid"=>$loginData,"reward"=>$str]);
       
        if($res) return res_success($res);
        return res_fail('更新失败');
    }

    //获取个人中奖信息
    public function getMyReward(){
        $loginData = Session::get('userid');
        $res = RecordModel::get(['uid'=>$loginData]);
        if($res) return res_success($res);
        return res_fail('未找到信息');
    }

}